package client.gui;

import javax.swing.*;

import sharedspace.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
 * This JFrame is a frame for Teacher to see student exam test and fill comments
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */

public class GUI_Teacher_StudentTestView extends JFrame {
	private static final long serialVersionUID = 1L;
	private int row;
	private List<Choices> choices;
	private String[] comments = {null};
	private String str;
	private JButton comment;
	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private Dimension scrnsize = toolkit.getScreenSize();
	private JLabel question = null;
	private JRadioButton answer = null;
	private JPanel form = null;
	private JScrollPane scroller = null;
	private Client_GUI ms;
	
	public GUI_Teacher_StudentTestView (Client_GUI ms,List<Choices> choices,int i)
	{
		super();
		this.choices = choices;
		this.row = i;
		this.ms = ms;
		initialize();
	}
	
	public void initialize()
	{
		this.setSize(600, 800);
		this.setLocation((toolkit.getScreenSize().width-600)/2, (toolkit.getScreenSize().height-800)/2);
		this.setTitle("Test");
		this.setLayout(null);
		displayTest();
		this.add(scroller,null);
	}
	
	public void displayTest()
	{
		// making new JPanel for test
		int flag = 0;
		form = new JPanel(new VerticalLayout(50,VerticalLayout.LEFT));
		form.setBackground(Color.WHITE);
		JLabel studentName = new JLabel();
		studentName.setText("Student ID: "+choices.get(0).getStudentID());
		form.add(studentName);
		comments = new String[choices.size()];
		for(int i = 0; i<choices.size(); i++)
		{
			JPanel qPanel = new JPanel(new VerticalLayout(0, VerticalLayout.LEFT));
			qPanel.setBackground(Color.WHITE);
			question = new JLabel((i+1)+". "+choices.get(i).getQuestion().getQuestion());
			question.setFont(new Font("Arial",Font.PLAIN,32));
			question.setForeground(Color.BLACK);
			ButtonGroup buttonGroup = new ButtonGroup();
			qPanel.add(question);// insert question to question JPanel
			question = new JLabel(" " + choices.get(i).getQuestion().getQuestionCommet());
			qPanel.add(question);// insert questionComment to question JPanel
			int choice = choices.get(i).getChoice();
			for (int j = 0; j<4; j++)
			{   //making JRadioButton for answers
				answer = new JRadioButton((j+1)+".     "+choices.get(i).getQuestion().getAnswers()[j]);
				answer.setActionCommand(Integer.toString(i));
				answer.setBackground(null);
				if (j+1==choice)
					answer.setSelected(true);
				else answer.setEnabled(false);
				if (j+1==choice && choice!=choices.get(i).getQuestion().getCorrectAnswer())
					{
						answer.setBackground(Color.RED);
						comment = new JButton("Add comment for question "+(i+1));
						comment.setActionCommand(i+"");
						flag = 1;
						
					}
				if (j+1==choices.get(i).getQuestion().getCorrectAnswer())
					answer.setBackground(Color.YELLOW);
				qPanel.add(answer);
				buttonGroup.add(answer);
				
			}
			
			if (flag == 1)
				qPanel.add(getComment());
			flag=0;
			
			
			
			form.add(qPanel);
			
		}
		scroller= new JScrollPane(form);
		scroller.setBounds(new Rectangle(0, 0, 600, 780));
	}
	
	public JButton getComment()
	{
		
		comment.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				str = JOptionPane.showInputDialog("Add comment for question");
				int i=Integer.parseInt(arg0.getActionCommand());
				ms.tag.aList.get(row).setTeacherComment_i(str, i);
				//comments[i] = str;
			}
			
		});
		
		return comment;
	}
	
	public String[] getComments()
	{
		return this.comments;
	}

}
